/**
 * 
 */
package com.redpixy.monit.start;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.redpixy.monit.common.Address;
import com.redpixy.monit.common.param.CheckAddressParam;
import com.redpixy.monit.common.param.MailParam;
import com.redpixy.monit.common.util.DateUtil;
import com.redpixy.monit.schedule.MonitDailyReport;
import com.redpixy.monit.schedule.MonitSchedule;

/**
 * @author admin
 *
 */
public class StartMonit {
	private static final Logger log = LoggerFactory
	.getLogger(MonitSchedule.class);
  private  static ScheduledExecutorService exeService = Executors.newSingleThreadScheduledExecutor();
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		setCommonLog();
		initAddress();
		exeService.scheduleAtFixedRate(new MonitSchedule(),5, CheckAddressParam.CHECKTIME, TimeUnit.SECONDS);
	    log.info("----->检测服务启动......");
		long delay = DateUtil.getDelayTimeInMills(CheckAddressParam.DAILAYREPORTTIME);
		exeService.scheduleAtFixedRate(new MonitDailyReport(),delay/1000, 3600*24, TimeUnit.SECONDS);
		log.info("----->日报服务启动......");

	}
	
	private static void setCommonLog(){
		System.setProperty(
				"org.apache.commons.logging.Log", 
				"org.apache.commons.logging.impl.SimpleLog");
				System.setProperty(
				"org.apache.commons.logging.simplelog.showdatetime", 
				"true");
				System.setProperty(
				"org.apache.commons.logging" + 
				".simplelog.log.org.apache.commons.httpclient", 
				"error");
	}
	
	private static void initAddress(){
		for (Element el : CheckAddressParam.URL) {
			String url = el.getTextTrim();
			String name = el.attributeValue("name");
			Address ad = new Address();
			ad.setAddressType(1);
			ad.setUrl(url);
			ad.setName(name);
			MailParam.CHECK_ADRESS.add( ad);
			}
		
		for (Element el : CheckAddressParam.IP) {
			String ip = el.getTextTrim().split(":")[0];
			String port = (el.getTextTrim().split(":")[1]);
			String name = el.attributeValue("name");
			Address ad = new Address();
			ad.setAddressType(2);
			ad.setIp(ip);
			ad.setPort(port);
			ad.setName(name);
			MailParam.CHECK_ADRESS.add(ad);
		}
	}
	
}
